package com.ultrapower.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ultrapower.commons.base.BaseController;
import com.ultrapower.commons.result.PageInfo;
import com.ultrapower.commons.utils.StringUtils;
import com.ultrapower.model.CallratioAnalysis;
import com.ultrapower.model.Result;
import com.ultrapower.service.ICallRatioService;
import com.ultrapower.service.IResultService;

@Controller
@RequestMapping("/callRatio")
public class CallRatioController extends BaseController{
	
	@Autowired
    private ICallRatioService callRatioService;
	@Autowired
    private IResultService resultService;

	/**
     * 通话占比分析管理页
     *
     * @return
     */
    @GetMapping("/manager")
    public String manager() {
        return "admin/callRatioAnalysis/callRatioAnalysis";
    }
    
    /**
     * 添加通话占比分析页
     *
     * @return
     */
    @GetMapping("/addCallRatio")
    public String addCallRatio() {
        return "admin/callRatioAnalysis/callRatioAnalysisAdd";
    }
    
    /**
     * 通话占比分析列表
     *
     * @param userVo
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(CallratioAnalysis callRatio, Integer page, Integer limit, String sort, String order) {
    	sort = "createTime";
    	order = "asc";
        PageInfo pageInfo = new PageInfo(page, limit, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        if (StringUtils.isNotBlank(callRatio.getName())) {
            condition.put("name", callRatio.getName());
        }
        pageInfo.setCondition(condition);
        callRatioService.selectDataGrid(pageInfo);
        return pageInfo;
    }
    
    /**
     * 添加场景
     *
     * @param callRatio
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(CallratioAnalysis callRatio) {
    	callRatio.setCreateTime(new Date());
        callRatioService.insert(callRatio);
        return renderSuccess("添加成功！");
    }
    
    /**
     * 删除场景
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(int id) {
    	callRatioService.deleteById(id);
        return renderSuccess("删除成功！");
    }
    
    /**
     * 编辑场景页
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, int id) {
    	CallratioAnalysis callRatio = callRatioService.selectById(id);
        model.addAttribute("callRatio", callRatio);
        return "admin/callRatioAnalysis/callRatioAnalysisEdit";
    }
    
    /**
     * 编辑场景
     *
     * @param organization
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(CallratioAnalysis callRatio) {
    	callRatioService.updateById(callRatio);
        return renderSuccess("编辑成功！");
    }
    
    /**
     * 场景评分页
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/gratePage")
    public String gratePage(Model model) {
//    	CallratioAnalysis callRatio = callRatioService.selectById(id);
//        model.addAttribute("callRatio", callRatio);
        return "admin/callRatioAnalysis/callRatioAnalysisGrate";
    }
    
    /**
     * 场景评分
     *
     * @param organization
     * @return
     */
    @RequestMapping("/grate")
    @ResponseBody
    public Object grate(CallratioAnalysis callRatio) {
    	callRatioService.updateById(callRatio);
        return renderSuccess("评分成功！");
    }
    
    /**
     * 场景数据查询结果页
     *
     * @return
     */
    @GetMapping("/showResult")
    public String showResult() {
        return "admin/callRatioAnalysis/callRatioResult";
    }
    
    /**
     * 场景列表数据查询结果
     *
     * @param Result
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     */
    @PostMapping("/showResultData")
    @ResponseBody
    public Object showResultData(Result result, Integer page, Integer limit, String sort, String order) {
    	sort = "id";
    	order = "asc";
        PageInfo pageInfo = new PageInfo(page, limit, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        CallratioAnalysis callRatio = callRatioService.selectById(result.getId());
        //这里对callRatio对象进行分析,然后在数据库中查出对应属性的sql
        //用户归属地市
        if (StringUtils.isNotBlank(callRatio.getCallingCity())) {
            condition.put("city", callRatio.getCallingCity());
        }
        //用户漫游地
        if (StringUtils.isNotBlank(callRatio.getRoamCity())) {
            condition.put("roamCity", callRatio.getRoamCity());
        }
        //性别
        if (0 != callRatio.getCallingSex()) {
            condition.put("sex", callRatio.getCallingSex());
        }
        //年龄
        if (StringUtils.isNotBlank(callRatio.getCallingAge())) {
        	if("0".equals(callRatio.getCallingAge())){
        		
        	}else{
        		condition.put("startcallingAge", callRatio.getCallingAge().split("-")[0]);
        		condition.put("endcallingAge", callRatio.getCallingAge().split("-")[1]);
        	}
            condition.put("age", callRatio.getCallingAge());
        }
        //主叫拨打频次
        if (StringUtils.isNotBlank(callRatio.getCallingFrequency())) {
        	if("0".equals(callRatio.getCallingFrequency())){
        		
        	}else{
        		condition.put("startcallingFrequency", callRatio.getCallingFrequency().split("-")[0]);
        		condition.put("endcallingFrequency", callRatio.getCallingFrequency().split("-")[1]);
        	}
        }
        //主叫号码被叫频次
        if (StringUtils.isNotBlank(callRatio.getCalledFrequency())) {
        	if("0".equals(callRatio.getCalledFrequency())){
        		
        	}else{
        		condition.put("startcalledFrequency", callRatio.getCalledFrequency().split("-")[0]);
        		condition.put("endcalledFrequency", callRatio.getCalledFrequency().split("-")[1]);
        	}
        }
        //时间
        if (StringUtils.isNotBlank(callRatio.getCallingTime())) {
            condition.put("startTime", callRatio.getCallingTime().split(" - ")[0]);
            condition.put("endTime", callRatio.getCallingTime().split(" - ")[1]);
        }
        if (StringUtils.isNotBlank(result.getTelephone())) {
            condition.put("telephone", result.getTelephone());
        }
        pageInfo.setCondition(condition);
        resultService.selectDataGrid(pageInfo);
        return pageInfo;
    }
    
    /**
     * 获取场景总数
     *
     * @return
     */
    @GetMapping("/sceneListCount")
    @ResponseBody
    public int sceneListCount() {
        int count = callRatioService.selectListCount();
        return count;
    }
}
